package scribes.mybus.ejb;

import javax.ejb.ActivationConfigProperty;


import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;


/**
 * Message-Driven Bean implementation class for: MyBusUploadBean
 *
 */
@MessageDriven(activationConfig = {
		@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.queue"),
		@ActivationConfigProperty(propertyName = "destination", propertyValue = "scribes/logging"),
		@ActivationConfigProperty(propertyName = "maxSession", propertyValue = "10") })
public class MyBusUploadBean implements MessageListener {
	@PersistenceContext(unitName = "wmbDb")
	private EntityManager data;

	
    /**
     * Default constructor. 
     */
    public MyBusUploadBean() {
        // TODO Auto-generated constructor stub
    }
	
	/**
     * @see MessageListener#onMessage(Message)
     */
    public void onMessage(Message message) {
        // TODO Auto-generated method stub
    	try {
			javax.jms.MapMessage om = (MapMessage) message;
			
			String fileName = om.getString("fileName");
			parseFile(fileName);
			
		} catch (JMSException e) {
			e.printStackTrace();
		}  
    }

	private void parseFile(String fileName) {
		// TODO Auto-generated method stub
		FileParser fileparser = new FileParser();
		fileparser.setData(data);
		
		fileparser.parseBusDataDetails(fileName);
		
	}

}
